// mircoApp/giftPackage/giftIndex/giftIndex.js
// import tool from "../../utils/util";
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
let app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    imgPath: app.globalData.imgPath,
    giftInfo:{},
    giftNum:1,
    packageTitle:'',
    packageId:'',
    clientWidths: "",
    giftList:[],
    goodsImage:'',
    page_template: 1,
    combination:{},
    otherPackages: [],
    //显示购结束时间
    timeStatus: false,
    endTime: 0,
    endTimeStr: "",
    end_time_obj:'',
    language:"",
    Hei:'', //轮播图自适应图片
    imgheights:[],
    imgBoxSize:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      packageId:options.id
    })
  },
  link({ currentTarget }) {
    // 点击轮播图跳转
    let url = currentTarget.dataset.item.carousel_link;
    if(url) {
      wx.navigateTo({ url });
    }
  },
  imgH(e){
    var winWid = wx.getSystemInfoSync().windowWidth;//获取当前屏幕的宽度
    var imgh=e.detail.height;//图片高度
    var imgw=e.detail.width;
    var swiperH=winWid*imgh/imgw;　//等比设置swiper的高度。  即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度    ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    this.data.imgheights.push(swiperH);
    this.data.imgBoxSize = Math.max.apply(null, this.data.imgheights);
    this.setData({
        Hei:`${this.data.imgBoxSize}px`//设置高度
    })
  },
  getdata(packageId) {
    let url = app.getNetAddresss("plugin.goods-package.frontend.package.package.index");
    app._postNetWork({
      showToastIn: false,
      url,
      data: { package_id:packageId },
      success: ({ data }) => {
        if (data.result) {
          let res = data.data;
          this.setData({
            carousels:res.carousels,
            packageTitle:res.title,
            combination:res.combination,
            otherPackages:res.other_packages,
            timeStatus:res.limit_time_status == 1 ? true : false,
            endTime:(res.end_time-parseInt(new Date().getTime() / 1000))*1000,
            giftInfo:res
          })
          if(res.title) {
            wx.setNavigationBarTitle({
              title: res.title
            })
          }
        } else {
          wx.showToast({
            icon: "none",
            title: data.msg,
            duration: 1500,
          });
          // wx.navigateTo({
          //   url: `/packageG/index/index`
          // });
        }
      }
    });
  },
  giftData() {
    let url = app.getNetAddresss("plugin.goods-package.frontend.package.package.combination-goods");
    let json = {
      package_id: this.data.packageId,
    }
    app._postNetWork({
      showToastIn: false,
      url,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        let data = res.data;
        if (res.result) {
          this.setData({
            giftList:data.list,
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  countDown(event){
    let {days,hours,seconds,minutes} = event.detail;
    //倒计时时间'
    let end_time_obj = {
      days:days>=10?days:"0" + days,
      hours:hours>=10?hours:"0" + hours,
      seconds:seconds>=10?seconds:"0" + seconds,
      minutes:minutes>=10?minutes:"0" + minutes
    }
    this.setData({end_time_obj});
  },
  //倒计时结束后
  finish(){
    //倒计时结束
    this.setData({timeStatus:false});
  },
  //跳到礼包套餐商品列表
  gogoodsDetailBtn() {
    wx.navigateTo({
      url: `/mircoApp/giftPackage/giftGoodsList/giftGoodsList?id=${this.data.packageId}`
    });
  },
  gopackgoods(event){
    let {id,type} = event.currentTarget.dataset;
    if(type == 1) {
      wx.navigateTo({
        url: `/mircoApp/giftPackage/giftIndex/giftIndex?id=${id}`
      });
    } else {
      wx.navigateTo({
        url: `/packageF/packageGoodse/packageGoods?pid=${id}`
      });
    }
  },
  //提交订单
  submitGoods() {
    let json = {};
    let cartIds = [];
    json.package_id = this.data.packageId;
    json.goods_list = [];
    this.data.giftList.map((item,index) => {
      cartIds.push(item.goods?.id);
      let goodsJson = {};
      goodsJson.goods_id = item.goods?.id;
      goodsJson.option_id = '';
      if(item.goods?.has_option) {
        goodsJson.option_id = item.goods?.has_many_options[0].id;
      }
      goodsJson.total = 1;
      json.goods_list.push(goodsJson);
    })
    if (cartIds.length<=0) return app.tips(`请选择结算商品`);
    wx.navigateTo({
      url: `/packageD/buy/myOrder_v2/myOrder_v2?tag=giftBuy&goods_ids=${JSON.stringify(cartIds)}&packagJson=${JSON.stringify(json)}` 
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getdata(this.data.packageId);
    this.giftData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    return {
      path: "/mircoApp/giftPackage/giftIndex/giftIndex?id=" + this.data.packageId + "&mid=" + mid,
      title: this.data.giftInfo.share_title,
      imageUrl: this.data.giftInfo.share_thumb,
    };
  }
})